<template>
  <div :id="id" :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import resize from "./mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    id: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "200px",
    },
    height: {
      type: String,
      default: "200px",
    },
    totalProfit: {
      type: Array,
      default: () => [],
    },
    profit: {
      type: Array,
      default: () => [],
    },
    xdata: {
      type: Array,
      default: () => [],
    },
    avgProfit: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  mounted() {
    this.initChart();
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = this.$echarts.init(document.getElementById(this.id));
      const xData = this.xdata;
      const colors = ["#5470C6", "#91CC75"];
      //const xData = ["一", "二", "三", "四", "五"];
      this.chart.setOption({
        backgroundColor: "#fff",
        title: {
          text: "设备收益",
          x: "20",
          top: "20",
          textStyle: {
            fontSize: "18",
          },
          subtextStyle: {
            color: "#90979c",
            fontSize: "16",
          },
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            textStyle: {
              color: "#000",
            },
          },
        },
        grid: {
          left: "5%",
          right: "5%",
          borderWidth: 0,
          top: 150,
          bottom: 95,
        },
        legend: {
          left: "40%",
          top: "10%",
          textStyle: {
            color: "#90979c",
            fontSize: "20px",
          },
        },
        calculable: true,
        xAxis: [
          {
            type: "category",
            axisLine: {
              lineStyle: {
                color: "#90979c",
              },
            },
            splitLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            splitArea: {
              show: false,
            },
            axisLabel: {
              interval: 0,
            },
            data: xData,
          },
        ],
        yAxis: [
          {
            type: "value",
            splitLine: {
              show: false,
            },
            axisLine: {
              lineStyle: {
                color: "#90979c",
              },
            },
            axisTick: {
              show: false,
            },
            axisLabel: {
              interval: 0,
            },
            splitArea: {
              show: false,
            },
          },
        ],
        series: [
          {
            name: "收益",
            type: "bar",
            itemStyle: {
              normal: {
                color: colors[1],
                barBorderRadius: 0,
                label: {
                  show: true,
                  position: "top",
                  formatter(p) {
                    return p.value > 0 ? p.value : "";
                  },
                },
              },
            },
            data: this.profit,
            // data: [5, 7, 9, 10, 14, 20],
          },
          {
            name: "总收益",
            type: "bar",
            itemStyle: {
              normal: {
                color: colors[0],
                label: {
                  show: true,
                  // textStyle: {
                  //   color: "#fff",
                  // },
                  //position: "insideTop",
                  position: "top",
                  formatter(p) {
                    return p.value > 0 ? p.value : "";
                  },
                },
              },
            },
            data: this.totalProfit,
            // data: [10, 20, 30, 40, 50],
          },
        ],
      });
    },
  },
};
</script>
